package com.lins.wechat.demo;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @Author linshuo
 * @Date 2023/7/25 15:02
 */
@Component
@Configuration
public class WechatTemplateMessage {
    public static void main(String[] args) {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx45fcb686e5979d14"); //appID
        wxStorage.setSecret("51f69aa568e7b1c43bc204a7256a7a0b");//app密钥
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("date", "2023/7/27"),
                new WxMpTemplateData("city", "无锡"),
                new WxMpTemplateData("weather", "多云"),
                new WxMpTemplateData("remark", "我发出来了")
        );


        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("oPOcV6bFFuXj1OQrGcemZ_pEew44")//要推送的用户openid
                .data(data) //数据
                .templateId("bksriEsaKbx3GBrNukGoG7W_Ra9sqPRrPWnbUvph6M4")//模版id
                .url("http://www.baidu.com") // 点击详情跳转地址
                .build();
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功：" + msg);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
